package com.csdj.t309tgspringboot.controller;

import com.csdj.t309tgspringboot.common.WebResult;
import com.csdj.t309tgspringboot.entity.Comments;
import com.csdj.t309tgspringboot.entity.User;
import com.csdj.t309tgspringboot.service.ICommentsService;
import com.csdj.t309tgspringboot.service.IUserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private ICommentsService iCommentsService;
    @Resource
    private IUserService iUserService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private WebResult webResult;

    @ResponseBody
    @GetMapping("/insertc")
    public WebResult insertC(@RequestParam(value = "commentsTitle",required = false) String commentsTitle,
                             @RequestParam(value = "pid",required = false) Integer pid,
                             @RequestParam(value = "postId",required = false) Integer postId){
        Comments comments = new Comments();
        comments.setCommentsTitle(commentsTitle);
        comments.setCommentsPid(pid);
        comments.setUserId(Integer.parseInt(stringRedisTemplate.opsForValue().get("id")));
        comments.setPostId(postId);
        Integer result;
        webResult = new WebResult();
        if(comments.getCommentsTitle().length()>0){
            result = iCommentsService.insertC(comments);
            if(result>0){
                webResult.putData("result",result);
                return webResult;
            }
            return null;
        }
        webResult.putData("result",-1);
        return webResult;
    }

    @ResponseBody
    @GetMapping("/selc")
    public WebResult selC(){
        List<Comments> selCom = iCommentsService.selC();
        List<User> user = iUserService.selUser();
        webResult = new WebResult();
        if(user!=null){
            webResult.putData("user",user);
        }
        if(selCom!=null){
            webResult.putData("selCom",selCom);
        }
        return webResult;
    }

    /*@ResponseBody
    @GetMapping("selsonc")
    public WebResult selSonC(@RequestParam(value = "pid",required = false) Integer pid){
        List<Comments> selSonCom = iCommentsService.selSonC(pid);
        if(selSonCom!=null){
            webResult = new WebResult();
            webResult.putData("selSonCom",selSonCom);
            return webResult;
        }
        return null;
    }*/
}
